import java.awt.Image;


public class MgSoldier extends MgFighter implements MgCommon{
	private MgWeapon weapon;
	private int ammo;
	
	public MgSoldier(int x, int y, int ammo, Image originalImg,Image hurtImg, MgWeapon weapon){
		super(x,y,originalImg,hurtImg);
		this.ammo = ammo;
		setMaxHealth(20);
		this.weapon = weapon;
	}
	
	public void move(){
		if(x<fighters.get(0).getX())
			x+=movement;
		else if(x>fighters.get(0).getX())
			x-=movement;
		if(y<fighters.get(0).getY())
			y+=movement;
		else if(y>fighters.get(0).getY())
			y-=movement;
	}
	public void fire(){
		if(ammo<=0)
			movement=2;
		else if(getWeapon().getClip()>0){
			if(!getWeapon().getReload().isRunning()){
				if(weapon.fire()){
					int dx = fighters.get(0).getX()-x;
					int dy = fighters.get(0).getY()-y;
					if(dx==0 || dy==0){
						if(dx==0 && dy ==0)
							return;
						else if(dy == 0)
							dx/=Math.abs(dx);
						else if(dx == 0)
							dy/=Math.abs(dy);
					}else if(Math.abs((double)dx/(double)dy)<0.414){
						dx = 0;
						dy/=Math.abs(dy);
					}else if(Math.abs((double)dy/(double)dx)<0.414){
						dx/=Math.abs(dx);
						dy = 0;
					}else{
						dx/=Math.abs(dx);
						dy/=Math.abs(dy);
					}
					/* Try to get arbitrary direction, but the component imbalance caused too fast bullets.
			}else if(Math.abs(dx)<Math.abs(dy)){
				dy=(int)Math.round((double)dy/(double)dx);
				dx/=Math.abs(dx);
			}else if(Math.abs(dx)>Math.abs(dy)){
				dx=(int)Math.round((double)dx/(double)dy);
				dy/=Math.abs(dy);
			}else{
				dx/=Math.abs(dx);
				dy/=Math.abs(dy);
			}
			//normalize bulletSpeed
			int bulletSpeed = (int)Math.round(((double)weapon.getBulletSpeed())/Math.pow(dx,2)+Math.pow(dy, 2));
			if(bulletSpeed == 0)
				bulletSpeed = 1;
					 */
					projectiles.add(new MgProjectile(x+(img.getWidth(null)/2+2)*dx,y+(img.getHeight(null)/2+2)*dy,dx,dy,weapon.getBulletSpeed(),weapon.getDamage(),weapon.getBulletSize(),weapon.getBulletColor()));
				}
			}
		}else
			reload();
	}
	
	public MgWeapon getWeapon(){
		return weapon;
	}
	public void reload(){
		ammo+=getWeapon().reload(ammo);
	}
}
